﻿using System;
using System.Collections.Generic;
//using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace WordSmith_V1.Library.Model.EntityFrameWork
{
    public class WordSmithContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Credential> Credentials { get; set; }
        public DbSet<Book> Books { get; set; }
       public DbSet<Chapter> Chapters { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Status> Status { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Genre> Genres { get; set; }


        public WordSmithContext() : base("WordSmith")
        {
            Debug.Write(Database.Connection.ConnectionString);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<User>()
                    .HasMany(u => u.Interests)
                    .WithMany(u => u.Users)
                    .Map(m => 
                        {
                        m.ToTable("UserInterest");
                        m.MapLeftKey("UserId");
                        m.MapRightKey("GenreId");
                        });

            modelBuilder.Entity<User>()
                  .HasMany(u => u.BookFollowed).WithMany(u => u.Authors)                  
                  .Map(m =>
                  {
                      m.ToTable("UserBookInterest");
                      m.MapLeftKey("UserId");
                      m.MapRightKey("BookId");
                  });
        }

        public class WordSmithInitailizer : DropCreateDatabaseIfModelChanges<WordSmithContext>
        {
             protected override void Seed(WordSmithContext context)
            {
                new List<Genre>
                {
                    new Genre(                       
                             "Arts & Photography"
                            
                        ),
                        new Genre(                                "Computers & Internet"
                            
                        ),
                       new Genre(                                "Biographies & Memoirs"
                            
                        ),
                        new Genre(                                "Comics & Graphic Novels"
                            
                        ),
                        new Genre(                                "Religion"
                            
                        ),
                        new Genre(                                "Science"
                            
                        ),
                        new Genre(                                "Animals"
                            
                        ),
                        new Genre(                                "Adult"
                            
                        ),
                        new Genre(                                "Romance"
                            
                        ),
                        new Genre(                                "Comedy"
                            
                        ),new Genre(                                "Adventure"
                            
                        ),new Genre(                                "Comedy"
                            
                        ),new Genre(                                "Crime"
                            
                        )

                }.ForEach(b => context.Genres.Add(b));
             }
        }

    }
}
